﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Peekaboo2008.dao;
using Peekaboo2008.model;
using Peekaboo2008.common;
using System.Data.SqlClient;
namespace Peekaboo2008.view
{
    public partial class FormCongNo : Form
    {
        String MaLoaiCongNo;
        int flag = 0;
        public FormCongNo()
        {
            InitializeComponent();
            initLoaiCongNo();
            FormCongNoLoad();
        }
        private void initLoaiCongNo()
        {
            DataTable db = new DataTable();
            db = CongNoDAO.LoaiCongNo_GetAll();
            cbbLoaiCongNo.DataSource = db;
            flag = 1;
            cbbLoaiCongNo.ValueMember = "ID";
            cbbLoaiCongNo.DisplayMember = "Ten";
            cbbLoaiCongNo.SelectedIndex = 0;
            MaLoaiCongNo = cbbLoaiCongNo.SelectedValue.ToString();            
        }
        public void FormCongNoLoad()
        {
            DataTable dt = CongNoDAO.CongNo_GetAll(MaLoaiCongNo);
            dgvCongNo.DataSource = dt;
        }

        private void dgvCongNo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            FormCongNo frmCongNo = this;
            CongNoModel model = new CongNoModel();
            model.Id = dgvCongNo.CurrentRow.Cells["clmID"].Value.ToString();
            model.Ma_loai_cong_no=dgvCongNo.CurrentRow.Cells["clmMaLoaiCongNo"].Value.ToString();
            model.Ten = dgvCongNo.CurrentRow.Cells["clmTen"].Value.ToString();
            model.Ma_khach_hang = dgvCongNo.CurrentRow.Cells["clmMaKH"].Value.ToString();
            model.TenKH = dgvCongNo.CurrentRow.Cells["clmTenKH"].Value.ToString();
            model.Ma_ngoai_te = dgvCongNo.CurrentRow.Cells["clmMaNgoaiTe"].Value.ToString();
            model.Thanh_toan = dgvCongNo.CurrentRow.Cells["clmThanhToan"].Value.ToString();
            model.Ty_gia = dgvCongNo.CurrentRow.Cells["clmTyGia"].Value.ToString();
            model.Thanh_tien = dgvCongNo.CurrentRow.Cells["clmThanhTien"].Value.ToString();
            model.Ghi_chu = dgvCongNo.CurrentRow.Cells["clmGhiChu"].Value.ToString();
            model.NgayCapNhatCuoi = dgvCongNo.CurrentRow.Cells["clmNgayCapNhatCuoi"].Value.ToString();

            FormChiTietCongNo frmChiTietCongNo = new FormChiTietCongNo(model,ref frmCongNo );
            frmChiTietCongNo.ShowDialog();
        }

        private void tsbThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void cbbLoaiCongNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flag == 0)
            {

            }
            else
            {
                MaLoaiCongNo = cbbLoaiCongNo.SelectedValue.ToString();
                FormCongNoLoad();
            }
        }

        private void tsbThem_Click(object sender, EventArgs e)
        {
            FormCongNo frmCongNo = this;
            FormChiTietCongNo frmChiTietCongNo = new FormChiTietCongNo(ref frmCongNo);
            frmChiTietCongNo.ShowDialog();
        }

        private void FormCongNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                tsbThem.PerformClick();
            }
            else if (e.KeyCode == Keys.F4)
            {
                tsbThoat.PerformClick();
            }
        }

        private void FormCongNo_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormCongNo_KeyDown);
        }



    }
}
